package com.ray.auth.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ray.auth.system.service.ButtonService;
import com.ray.auth.system.service.DictService;
import com.ray.auth.system.service.DictTypeService;
import com.ray.auth.system.table.entity.Button;
import com.ray.auth.system.table.entity.Dict;
import com.ray.auth.system.table.entity.DictType;
import com.ray.common.util.DataGridUtil;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.web.controller.WebController;

/**
 * <h1>资源 控制器 </h1>
 * @创建人 ray
 * @创建时间 2017-07-31
 * @版本 1.0
 * @重要修改备注 
 * 
 */
@Controller
@RequestMapping("/system/dict")
public class DictController extends WebController{
	@Autowired
	private DictTypeService dictTypeService;
	@Autowired
	private DictService dictService;
	@Autowired
	private ButtonService buttonService;
	
	/**
	 * 字典页面跳转
	 * @return
	 */
	@RequestMapping(value = "view")
	@RequiresPermissions("dict:view")
	public ModelAndView view(String menuId, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.setViewName("system/dict/view");
	  return modelAndView;
	}
	/**
	 * 新增页面
	 * @return
	 */
	@RequestMapping(value = "typeAddView")
	@RequiresPermissions("dict:type:add")
	public ModelAndView typeAddView(String menuId,String parentTypeName,DictType record, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("parentTypeName", parentTypeName);
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("system/dict/type/add");
	  return modelAndView;
	}
	
	/**
	 * 类型修改页面
	 * @return
	 */
	@RequestMapping(value = "typeUpdateView")
	@RequiresPermissions("dict:type:update")
	public ModelAndView typeUpdateView(String menuId,String parentTypeId,String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  //查询菜单
	  DictType record = dictTypeService.queryDictType(id, user);
	  //获取父节点
	  DictType parentRecord = dictTypeService.queryParentDictType(parentTypeId, user);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("parentTypeName", parentRecord.getTypeName());
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("system/dict/type/update");
	  return modelAndView;
	}
	
	/**
	 * 新增页面
	 * @return
	 */
	@RequestMapping(value = "typeView")
	@RequiresPermissions("dict:type:view")
	public ModelAndView menuView(String menuId,String parentTypeName,String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  //查询菜单
	  DictType record =  dictTypeService.queryDictType(id, user);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("parentMenuName", parentTypeName);
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("system/dict/type/view");
	  return modelAndView;
	}
	
	/**
	 * 新增方法
	 * @return
	 */
	@RequestMapping(value = "typeUpdate")
	@RequiresPermissions("dict:type:update")
	public ResponseEntity<Result<Object>> typeUpdate(DictType record, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  Result<Object> result = dictTypeService.updateDictType(record, user);
	  return doResult(result);
	}
	
	/**
	 * 新增方法
	 * @return
	 */
	@RequestMapping(value = "typeAdd")
	@RequiresPermissions("dict:type:add")
	public ResponseEntity<Result<Object>> typeAdd(DictType record, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  Result<Object> result = dictTypeService.saveDictType(record, user);
	  return doResult(result);
	}
	
	/**
	 * 资源分页查询
	 * @return
	 */
	@RequestMapping(value = "query")
	@RequiresPermissions("dict:type:query")
	@ResponseBody
	public ResponseEntity<Map<String,Object>> query(DictType params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		PageDto<DictType> page = this.createJQPage(request);
		page = dictTypeService.queryDictTypeList(params, page, loginUser);
		return doSuccess(DataGridUtil.dataConvert(page));
	}
	/**
	 * 资源-按钮分页查询
	 * @return
	 */
	@RequestMapping(value = "dictQuery")
	@RequiresPermissions("dict:dict:query")
	@ResponseBody
	public ResponseEntity<Map<String,Object>> dictQuery(Dict params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		PageDto page = this.createJQPage(request);
		page = dictService.queryDict(params, page, loginUser);
		return doSuccess(DataGridUtil.dataConvert(page));
	}
	
	/**
	 * 菜单删除
	 * @return
	 */
	@RequestMapping(value = "typeDelete")
	@RequiresPermissions("dict:type:delete")
	@ResponseBody
	public ResponseEntity<Result<Object>> typeDelete(String id, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = dictTypeService.deleteDictType(id, loginUser);
		return doResult(result);
	}
	
	/**
	 * 菜单删除
	 * @return
	 */
	@RequestMapping(value = "dictDelete")
	@RequiresPermissions("dict:dict:delete")
	@ResponseBody
	public ResponseEntity<Result<Object>> dictDelete(String id, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = dictService.deleteDict(id, loginUser);
		return doResult(result);
	}
	
	/**
	 * 新增页面
	 * @return
	 */
	@RequestMapping(value = "dictAddView")
	@RequiresPermissions("dict:dict:add")
	public ModelAndView dictAddView(String menuId,String typeName,Dict record, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("typeName", typeName);
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("system/dict/dict/add");
	  return modelAndView;
	}
	
	/**
	 * 新增方法
	 * @return
	 */
	@RequestMapping(value = "dictAdd")
	@RequiresPermissions("dict:dict:add")
	public ResponseEntity<Result<Object>> dictAdd(Dict record, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  Result<Object> result = dictService.saveDict(record, user);
	  return doResult(result);
	}
	
	/**
	 * 修改页面
	 * @return
	 */
	@RequestMapping(value = "dictUpdateView")
	@RequiresPermissions("dict:dict:update")
	public ModelAndView buttonUpdateView(String menuId,String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  //查询菜单
	  Dict record = dictService.queryDict(id,user);
	  //获取父节点
	  DictType parentRecord = dictTypeService.queryDictType(record.getTypeId(), user);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("typeName", parentRecord.getTypeName());
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("system/dict/dict/update");
	  return modelAndView;
	}
	
	/**
	 * 更新方法
	 * @return
	 */
	@RequestMapping(value = "dictUpdate")
	@RequiresPermissions("dict:dict:update")
	public ResponseEntity<Result<Object>> buttonUpdate(Dict record, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  Result<Object> result = dictService.updateDict(record, user);
	  return doResult(result);
	}
}
